package com.syn.synold4s.adapter;

import static com.gaoyi.basemodule.baseUtil.OKHttpUtil.AI_SITE_IMG_URL;
import static com.gaoyi.basemodule.baseUtil.OKHttpUtil.BASE_NET_URL;
import static com.gaoyi.basemodule.baseUtil.OKHttpUtil.HELMET_IMG_URL;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.gaoyi.basemodule.baseAdapter.BaseAdapter;
import com.gaoyi.basemodule.baseAdapter.BaseViewHolder;
import com.gaoyi.basemodule.baseUtil.DateFormatUtil;
import com.gaoyi.basemodule.baseUtil.ImageUtil;
import com.gaoyi.basemodule.baseUtil.OKHttpUtil;
import com.gaoyi.basemodule.baseUtil.SPUtil;
import com.gaoyi.basemodule.baseconstant.SPName;
import com.syn.synold4s.R;
import com.syn.synold4s.activity.ImagePreviewActivity;
import com.syn.synold4s.bean.dto.UnSafeActEventDto;
import com.syn.synold4s.databinding.UnsafeActItemBinding;

import java.util.ArrayList;

public class UnsafeActAdapter extends BaseAdapter<UnsafeActItemBinding, UnSafeActEventDto> {

    private String aiSiteImgUrl;
    public UnsafeActAdapter(Context context) {
        super(context);
    }

    @Override
    protected void setBindingVariable(UnsafeActItemBinding binding, int position) {
    }

    @Override
    protected void onBindItem(UnsafeActItemBinding binding, UnSafeActEventDto item, int position) {
        if (TextUtils.isEmpty(item.getPhoto())) {
            binding.ivUnsafe.setImageResource(R.drawable.error_img);
        } else {
            aiSiteImgUrl = SPUtil.getInstance().getString(SPName.API_URL, BASE_NET_URL) + AI_SITE_IMG_URL;
            ImageUtil.loadImg(binding.ivUnsafe, aiSiteImgUrl + "800/" + item.getPhoto(), R.drawable.error_img);
        }
        binding.tvUnsafeName.setText(item.getDeviceName() != null ? item.getDeviceName() : "");
        binding.tvUnsafeTime.setText(DateFormatUtil.DateTimeToStr(item.getPortalAt(), "dd-MM-YYYY HH:mm:ss"));
    }

    @Override
    protected void onCreateView(UnsafeActItemBinding binding, BaseViewHolder<UnsafeActItemBinding> holder) {
        binding.ivUnsafe.setOnClickListener(view -> {
            ArrayList<String> photos = new ArrayList<>();
            photos.add(aiSiteImgUrl + "800/" + getPositionData(holder.getLayoutPosition()).getPhoto());
            ImagePreviewActivity.photoPreview(mContext, photos, 0);
        });
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.unsafe_act_item;
    }
}
